/*
 *	This file is part of Lounge-Cast.
 *
 *  Author:  	Markus Moormann
 *	E-mail:  	mmoorman@uni-osnabrueck.de
 *	Copyright:	University of Osnabrueck (Zentrum Virtuos)
 * 	Website:	www.uni-osnabrueck.de
 *	Date:		16.02.2011
 *
 *	Lounge-Cast is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *	Lounge-Cast is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Lounge-Cast.  If not, see <http://www.gnu.org/licenses/>.
 */
/*****************************************************
*  
*  Copyright 2010 Adobe Systems Incorporated.  All Rights Reserved.
*  
*****************************************************
*  The contents of this file are subject to the Mozilla Public License
*  Version 1.1 (the "License"); you may not use this file except in
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*   
*  Software distributed under the License is distributed on an "AS IS"
*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
*  License for the specific language governing rights and limitations
*  under the License.
*   
*  
*  The Initial Developer of the Original Code is Adobe Systems Incorporated.
*  Portions created by Adobe Systems Incorporated are Copyright (C) 2010 Adobe Systems 
*  Incorporated. All Rights Reserved. 
*  
*****************************************************/

package org.osmf.samples
{
	import mx.core.UIComponent;
	
	import org.osmf.containers.MediaContainer;
	import org.osmf.events.DisplayObjectEvent;
	import org.osmf.layout.LayoutRendererBase;
	import org.osmf.media.MediaElement;
	import org.osmf.media.MediaPlayer;

	/**
	 * UIComponent that exposes an OSMF MediaContainer.  Useful for integrating
	 * OSMF into a Flex application.
	 **/
	public class MediaContainerUIComponent extends UIComponent
	{
		// Public Interface
		//
		
		public function MediaContainerUIComponent()
		{
			mediaPlayer = new MediaPlayer();
			container = new MediaContainer();
			addChild(_container);
		}
		
		public function set media(media:MediaElement):void
		{
			mediaPlayer.media = media;
		}
		
		public function get media():MediaElement
		{
			return mediaPlayer.media;
		}
		
		public function get mediaPlayer():MediaPlayer
		{
			return _mediaPlayer;
		}
		
		public function set mediaPlayer(mediaPlayer:MediaPlayer):void
		{
			_mediaPlayer = mediaPlayer;
		}
		
		public function addMediaElement(element:MediaElement):void
		{
			container.addMediaElement(element);
		}
		
		public function removeMediaElement(element:MediaElement):void
		{
			container.removeMediaElement(element);
		}
		
		public function set container(value:MediaContainer):void
		{
			if (value != _container)
			{
				if (_container && contains(_container))
				{
					_container.removeEventListener(DisplayObjectEvent.MEDIA_SIZE_CHANGE, onMediaSizeChange);
					removeChild(_container);
				}
				
				_container = value;
				
				if (_container)
				{
					_container.addEventListener(DisplayObjectEvent.MEDIA_SIZE_CHANGE, onMediaSizeChange);
					_container.width = unscaledWidth;
					_container.height = unscaledHeight;
					addChild(_container);
				}
				
				invalidateSize();
				invalidateDisplayList();
			}
		}
		
		public function get container():MediaContainer
		{
			return _container;
		}
		
		// Overrides
		//
		
		override protected function measure():void
		{
			super.measure();
			
			if (_container)
			{
				var renderer:LayoutRendererBase = _container.layoutRenderer;
				if (renderer)
				{
					measuredWidth = renderer.measuredWidth;
					measuredHeight = renderer.measuredHeight;
				}				
			}
			if (measuredWidth == 0 || isNaN(measuredWidth) ||
				measuredHeight == 0 || isNaN(measuredHeight) )
			{
				measuredWidth = DEFAULT_WIDTH;
				measuredHeight = DEFAULT_HEIGHT;
			}
			
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			if (_container)
			{
				_container.width = unscaledWidth;
				_container.height = unscaledHeight;
			}
				
			super.updateDisplayList(unscaledWidth, unscaledHeight);
		}
		
		// Internals
		//
		
		private function onMediaSizeChange(event:DisplayObjectEvent):void
		{
			invalidateSize();
			invalidateDisplayList();
		}
		
		private static const DEFAULT_WIDTH:Number = 320;
		private static const DEFAULT_HEIGHT:Number = 240;
		

		private var _container:MediaContainer;
		private var _mediaPlayer:MediaPlayer;
	}
}